package com.shuan.reader;

import java.io.IOException;
import java.io.InputStream;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;
import org.springframework.lang.NonNull;
import org.springframework.core.type.classreading.SimpleMetadataReader;
/**
 * 
 * @author hehuosheng
 * @date 2025/8/31
 **/
public class DecryptedMetadataReaderFactory  extends SimpleMetadataReaderFactory {
    public DecryptedMetadataReaderFactory(ClassLoader classLoader) {
        super(classLoader);
    }

    @NonNull
    @Override
    public MetadataReader getMetadataReader(@NonNull String className) throws IOException {
        // 1. 获取原始类输入流（从类路径）
        InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(className.replace('.', '/') + ".class");
        if (inputStream == null) {
            throw new IOException("类文件未找到: " + className);
        }

        // 2. 使用自定义 ClassReader 解析解密后的类
        try (DecryptedClassReader classReader = new DecryptedClassReader(inputStream)) {
            return new SimpleMetadataReader(classReader, Thread.currentThread().getContextClassLoader());
        }
    }
}
